package com.javadaemon.Tinderbox.model;

import com.javadaemon.Tinderbox.Constants;
import com.javadaemon.Tinderbox.Tinderbox;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 * This file is part of Tinderbox.
 *
 * Tinderbox is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Ponix is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Tinderbox.  If not, see <http://www.gnu.org/licenses/>.
 *
 * This class is for general small methods, such as getHttpUserAgent().
 * 
 * @author Mads Horndrup
 * @author Gnarly
 */
public class Misc {

    public static Robot robot;
    private static SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy HH.mm.ss a");

    /*
     * Get a HTTP User Agent, based on your OS.
     * @author Gnarly
     */
    public static String getHttpUserAgent() {
        String os = System.getProperty("os.name").toLowerCase();
        String agent = "Mozilla/5.0 (X11; U; Linux i686; en-GB; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6";
        if (os.contains("mac")) {
            agent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-us) AppleWebKit/531.9 (KHTML, like Gecko) Version/4.0.3 Safari/531.9";
        } else if (os.contains("windows")) {
            agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6";
        }
        return agent;
    }

    public static String trim(final String str, final char ch) {
        if ((str == null) || str.isEmpty()) {
            return str;
        } else if (str.length() == 1) {
            return str.charAt(0) == ch ? "" : str;
        }
        try {
            final int l = str.length() - 1;
            if (str.charAt(l) == ch) {
                return str.substring(0, l);
            }
            return str;
        } catch (final Exception e) {
            return str;
        }
    }

    /**
     * Gets the current version from the website
     * @return
     *      the version, as a double.
     */
    public static double getVersion() {
        BufferedReader r = null;
        try {
            r = new BufferedReader(new InputStreamReader(new URL("http://www.javadaemon.com/tinderbox/version.txt").openStream()));
            double version = Double.parseDouble(r.readLine());
            r.close();
            return version;
        } catch (IOException ex) {
            Logger.getLogger(Misc.class.getName()).log(Level.SEVERE, null, ex);
        }
        return Constants.VERSION;
    }

    /*
     * Creates a screenshot at the specified directory
     * @return Returns the path of the newly created screenshot
     */
    public static void createScreenshot() {
        try {
            if (robot == null) {
                robot = new Robot();
            }
            Rectangle size = getSize();
            BufferedImage image = robot.createScreenCapture(size);
            String path = "" + System.getProperty("user.home") + "/Tinderbox/Screenshots";
            File pathname = new File(path);
            if (!pathname.exists()) {
                pathname.mkdirs();
            }
            String filename = path + "/" + format.format(new Date()) + ".png";
            File file = new File(filename);
            ImageIO.write(image, "png", file);
        } catch (Exception i) {
        }
    }

    private static Rectangle getSize() {
        Rectangle size = Tinderbox.mainFrame.getBounds();
        if (Tinderbox.getLoader() != null) {
            Point p = Tinderbox.getLoader().getApplet().getLocationOnScreen();
            Rectangle panelsize = Tinderbox.getLoader().getApplet().getBounds();
            size = new Rectangle((int) p.getX(), (int) p.getY(), (int) panelsize.getWidth(), (int) panelsize.getHeight());
        }
        if (Tinderbox.isFullscreen()) {
            size = Tinderbox.window.getBounds();
        }
        return size;
    }
}
